{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Streaming Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This section is about **real-time or streaming data**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import fxcmpy\n",
    "import pandas as pd\n",
    "import datetime as dt\n",
    "con = fxcmpy.fxcmpy(config_file='fxcm.cfg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Approach"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Streaming the current market data is straightfoward, one just need to **subscribe** to the instrument of interest."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "con.subscribe_market_data('EUR/USD')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A list with the subscribed instruments is returned by the `con.get_subscribed_symbols()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['EUR/USD']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_subscribed_symbols()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One can also check the subscription for a specified instruments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.is_subscribed('EUR/USD')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After the subscription, `fxcm.py` collects the data in a `pandas` `DataFrame`. The `con.get_prices()` method returns that `DataFrame` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Bid</th>\n",
       "      <th>Ask</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-01-09 13:43:58.637</th>\n",
       "      <td>1.19199</td>\n",
       "      <td>1.19222</td>\n",
       "      <td>1.19769</td>\n",
       "      <td>1.19144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-09 13:43:59.144</th>\n",
       "      <td>1.19198</td>\n",
       "      <td>1.19222</td>\n",
       "      <td>1.19769</td>\n",
       "      <td>1.19144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-09 13:44:00.080</th>\n",
       "      <td>1.19200</td>\n",
       "      <td>1.19223</td>\n",
       "      <td>1.19769</td>\n",
       "      <td>1.19144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-09 13:44:04.466</th>\n",
       "      <td>1.19200</td>\n",
       "      <td>1.19224</td>\n",
       "      <td>1.19769</td>\n",
       "      <td>1.19144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-09 13:44:05.965</th>\n",
       "      <td>1.19200</td>\n",
       "      <td>1.19222</td>\n",
       "      <td>1.19769</td>\n",
       "      <td>1.19144</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             Bid      Ask     High      Low\n",
       "2018-01-09 13:43:58.637  1.19199  1.19222  1.19769  1.19144\n",
       "2018-01-09 13:43:59.144  1.19198  1.19222  1.19769  1.19144\n",
       "2018-01-09 13:44:00.080  1.19200  1.19223  1.19769  1.19144\n",
       "2018-01-09 13:44:04.466  1.19200  1.19224  1.19769  1.19144\n",
       "2018-01-09 13:44:05.965  1.19200  1.19222  1.19769  1.19144"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_prices('EUR/USD')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also fetch only the last available quotes via `con.get_last_price()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Bid     1.19200\n",
       "Ask     1.19223\n",
       "High    1.19769\n",
       "Low     1.19144\n",
       "Name: 2018-01-09 13:44:09.837000, dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_last_price('EUR/USD')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To stop the stream and delete (!) the dataframe, use `con.unsubscribe_market_data()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "con.unsubscribe_market_data('EUR/USD')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Bid</th>\n",
       "      <th>Ask</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [Bid, Ask, High, Low]\n",
       "Index: []"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_prices('EUR/USD')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Callback Functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One can add a `list` object with **callback functions** to the subscription, as in the example below.\n",
    "\n",
    "The function `print_data()` simply prints selected quotes as soon as they are retrieved."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def print_data(data, dataframe):\n",
    "    print('%3d | %s | %s, %s, %s, %s, %s' \n",
    "          % (len(dataframe), data['Symbol'], \n",
    "             pd.to_datetime(int(data['Updated']), unit='ms'), \n",
    "             data['Rates'][0], data['Rates'][1], data['Rates'][2],\n",
    "             data['Rates'][3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  1 | new values received for EUR/USD | 2018-01-09 13:44:22.201000, 1.1921, 1.19234, 1.19769, 1.19144\n",
      "  2 | new values received for EUR/USD | 2018-01-09 13:44:22.745000, 1.19214, 1.19237, 1.19769, 1.19144\n",
      "  3 | new values received for EUR/USD | 2018-01-09 13:44:23.195000, 1.19214, 1.19237, 1.19769, 1.19144\n",
      "  4 | new values received for EUR/USD | 2018-01-09 13:44:23.753000, 1.19214, 1.19237, 1.19769, 1.19144\n"
     ]
    }
   ],
   "source": [
    "con.subscribe_market_data('EUR/USD', (print_data,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "con.unsubscribe_market_data('EUR/USD')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
